package com.ss.android.article.base.ui;

import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;

public class ImeFrameLayout extends FrameLayout implements Callback {
    public interface OnImeEventListener {
        void i();
    }

    OnImeEventListener mOnImeEventListener;
    private WeakHandler mWeakHandler;

    public ImeFrameLayout(Context context) {
        super(context);
        this.mWeakHandler = new WeakHandler(this);
    }

    public ImeFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mWeakHandler = new WeakHandler(this);
    }

    public ImeFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mWeakHandler = new WeakHandler(this);
    }

    public boolean dispatchKeyEventPreIme(KeyEvent keyEvent) {
        if (Logger.debug()) {
            Logger.d("ImeFrameLayout", "dispatchKeyEventPreIme(" + keyEvent + ")");
        }

        if (keyEvent.getKeyCode() == 4) {
            KeyEvent.DispatcherState dispatcherState = this.getKeyDispatcherState();
            if (dispatcherState != null) {
                if (keyEvent.getAction() == 0 && keyEvent.getRepeatCount() == 0) {
                    dispatcherState.startTracking(keyEvent, this);
                    return true;
                }
                if (keyEvent.getAction() != 1) {
                    return super.dispatchKeyEventPreIme(keyEvent);
                }
                if (keyEvent.isCanceled()) {
                    return super.dispatchKeyEventPreIme(keyEvent);
                }
                if (!dispatcherState.isTracking(keyEvent)) {
                    return super.dispatchKeyEventPreIme(keyEvent);
                }
                this.mWeakHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mOnImeEventListener.i();
                    }
                });
                return true;
            } else {
                return super.dispatchKeyEventPreIme(keyEvent);
            }
        } else {
            return super.dispatchKeyEventPreIme(keyEvent);
        }
    }

    int getWindowHeight() {
        return ((WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
    }

    public void handleMsg(Message message) {
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (Logger.debug()) {
            Logger.d("ImeFrameLayout", String.format("h-oldh=%d,     (w=%d, h=%d, oldw=%d, oldh=%d)", Integer.valueOf(h - oldh), Integer.valueOf(w), Integer.valueOf(h), Integer.valueOf(oldw), Integer.valueOf(oldh)));
        }

        if (this.mOnImeEventListener != null) {
            this.mWeakHandler.post(new p(this, oldh, h));
        }
    }

    public void setOnImeEventListener(OnImeEventListener onImeEventListener) {
        this.mOnImeEventListener = onImeEventListener;
    }
}

